﻿/** DETAILS AND USAGE INSTRUCTIONS ^
 * 
 * @class: com.singlenotion.display.buttons.LibraryAssetButton
 * @copyright: (c) Copyright 2009 Single Notion.
 * @tiptext: Basic button class that allows skinning from library elements.
 * 
 * @language: Actionscript 3
 * 
 * @author: Adam Jowett
 * @company: Single Notion
 * @link: http://www.singlenotion.com
 * 
 * @created: 23/06/2009
 * @modified: 23/06/2009
 * @version: 1.0
 * 
 * @description:	
 * 
 * @usage:
 * 
 * 		To create a new basic button with this class (assuming we have four movieclips or sprites in the library with
 * 		them exporting for actionscript and class names of upBackground, overBackground, downBackground and outBackground.
 * 
 * 			myButton = new LibraryAssetButton(upBackground,overBackground,downBackground,outBackground);
 * 			myButton.x = 30;
 * 			addChild(myButton);
 * 
 * @parameter: 		upSkin:Class - linkage name of library element for up state
 * @parameter: 		overSkin:Class - linkage name of library element for over state
 * @parameter:		downSkin:Class - linkage name of library element for down state
 * @parameter:		outSkin:Class - linkage name of library element for out state
 * @parameter:		locked:Boolean - whether you want the button accesible by default
 * 
 * @method:			lock();
 * @method:			unlock();
 * 
 * @returns:		nothing.
 * 
 */

package com.singlenotion.display.buttons
{
	import com.singlenotion.events.CustomEvent.v01.CustomEvent;
	import flash.display.MovieClip;	
	import flash.events.MouseEvent;
	import flash.ui.Mouse;
   
	public class LibraryAssetButton extends MovieClip 
	{		
		private var _upSkin:Class;
		private var _overSkin:Class;
		private var _downSkin:Class;
		private var _outSkin:Class;
		
		private var _locked:Boolean;
	  
		private var _skin:MovieClip;
	  
		public function LibraryAssetButton ($upSkin:Class, $overSkin:Class, $downSkin:Class, $outSkin:Class, $locked:Boolean) 
		{
			_upSkin = $upSkin;
			_overSkin = $overSkin;
			_downSkin = $downSkin;
			_outSkin = $outSkin;
			_locked = $locked;
			
			updateUp();
			(locked) ? lock() : unlock();
			
			init();
		}
	  
		protected function init ():void 
		{
		}
	  
		protected function pressListener ($e:MouseEvent):void
		{
			dispatchEvent(new CustomEvent(ButtonEvents.ON_PRESS,false,false,{}));
			updateDown();
		}
	  
		protected function releaseListener ($e:MouseEvent):void 
		{
			dispatchEvent(new CustomEvent(ButtonEvents.ON_RELEASE, false, false, { } ));
			updateOver();
		}
	  
		protected function rollOverListener ($e:MouseEvent):void
		{
			dispatchEvent(new CustomEvent(ButtonEvents.ON_ROLLOVER, false, false, { } ));
			updateOver();
			removeEventListener(MouseEvent.MOUSE_OVER, rollOverListener);
		}
	  
		protected function rollOutListener ($e:MouseEvent):void
		{
			dispatchEvent(new CustomEvent(ButtonEvents.ON_ROLLOUT, false, false, { } ));
			updateUp();
			if (!locked) addEventListener(MouseEvent.MOUSE_OVER, rollOverListener, false, 0, true);
		}
		
		protected function updateUp ():void
		{
			updateSkin(_upSkin);
		}
	  
		protected function updateOver ():void
		{
			updateSkin(_overSkin);
		}
	  
		protected function updateDown ():void
		{
			updateSkin(_downSkin);
		}
	  
		protected function updateOut ():void
		{
			updateSkin(_outSkin);
		}
	  
		protected function updateSkin ($skin:Class):void
		{
			if (_skin != null) removeSkin();
			_skin = new $skin();
			addChild(_skin);
		}
	  
		protected function removeSkin ():void
		{
			removeChild(_skin);
			_skin = null;
		}
	  
		protected function addListeners ():void
		{
			addEventListener(MouseEvent.MOUSE_DOWN, pressListener, false, 0, true);
			addEventListener(MouseEvent.MOUSE_UP, releaseListener, false, 0, true);
			addEventListener(MouseEvent.MOUSE_OVER, rollOverListener, false, 0, true);
			addEventListener(MouseEvent.MOUSE_OUT, rollOutListener, false, 0, true);
		}
	  
		protected function removeListeners ():void
		{
			removeEventListener(MouseEvent.MOUSE_DOWN, pressListener);
			removeEventListener(MouseEvent.MOUSE_UP, releaseListener);
			removeEventListener(MouseEvent.MOUSE_OVER, rollOverListener);
			removeEventListener(MouseEvent.MOUSE_OUT, rollOutListener);
		}
	  
		public function lock ():void
		{
			updateSkin(_outSkin);
			buttonMode = false;
			locked = true;
			removeListeners();
		}
	  
		public function unlock ():void
		{
			buttonMode = true;
			locked = false;
			addListeners();
			updateSkin(_upSkin)
		}
	  
		public function get locked ():Boolean {	return(_locked); }
	  
		public function set locked ($val:Boolean):void { _locked = $val; }
	}
}